import time
import pytesseract
from PIL import Image, ImageEnhance

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/bin/tesseract'
# from selenium import webdriver
#  
# browser = webdriver.Safari()
# browser.maximize_window()
# browser.get("http://221.226.118.168:7080/jscbmis/admin/appsystem/html/entp_creditQuery.html")
# browser.find_element_by_id("edentpname").send_keys("公司")
# browser.find_element_by_name("validcode").send_keys("afff")
# # browser.find_element_by_id("validcodeimg").
# browser.save_screenshot("/Users/wangshuguan/Desktop/backup01.png")
ran = Image.open("/Users/wangshuguan/Desktop/backup01.png")
box = (590, 99, 671, 126)  # 获取验证码位置,自动定位不是很明白，就使用了手动定位，代表（左，上，右，下）
ran.crop(box).save("/Users/wangshuguan/Desktop/backup02.png")
imageCode = Image.open("/Users/wangshuguan/Desktop/backup02.png") # 图像增强，二值化
# imageCode.load()
sharp_img = ImageEnhance.Contrast(imageCode).enhance(2.0)
sharp_img.save("/Users/wangshuguan/Desktop/backup03.png")
sharp_img.load()  # 对比度增强
time.sleep(2)
print(sharp_img)
# sharp_img.show()
code = pytesseract.image_to_string(sharp_img.resize((81*2,27*2)),config="--tessdata-dir /Users/wangshuguan/tessdata --psm 7 -l eng+snum")
# 5、收到验证码，进行输入验证
print("code:"+code)



# browser.find_element_by_id("pwd").send_keys("wsg123456")
# browser.find_element_by_id("account").send_keys("cxj")
# browser.find_element_by_id("pwd").send_keys("cxj@666")
# browser.execute_script("login()")



# import urllib.request
# import io
# from PIL import Image

# image_bytes = urllib.request.urlopen("http://221.226.118.168:7080/jscbmis/servlet/ValidCodeServlet").read()
# data_stream = io.BytesIO(image_bytes)
# pil_image = Image.open(data_stream)
# print(pil_image)
